package com.xiaoyu.serviceImpl;

import com.xiaoyu.AccountDao;
import com.xiaoyu.PostDao;
import com.xiaoyu.ScoreDao;
import com.xiaoyu.StudentDao;
import com.xiaoyu.client.Account_PostDao;
import com.xiaoyu.model.Account;
import com.xiaoyu.model.Post;
import com.xiaoyu.model.Score;
import com.xiaoyu.model.Student;
import com.xiaoyu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private PostDao postDao;
    @Autowired
    private Account_PostDao accountPostDao;
    @Autowired
    private ScoreDao scoreDao;

    @Override
    public List<Student> allStudent(Student student) {
        return studentDao.findStudent(student);
    }

    @Override
    public Boolean deleteStudent(Student student) {
        //删除学生信息
        studentDao.deleteStudent(student);
        //删除成绩信息
        Score score=new Score();
        score.setSid(student.getId());
        scoreDao.deleteScore(score);

        if(studentDao.findStudent(student).size()==0)
            return true;
        return false;
    }

    @Override
    public Boolean addStudent(Student student,Account account) {
        //获取aid
        Account a=accountDao.findOneAccount(account);
        student.setAid(a.getId());

        //设置账户权限
        Post p=new Post();
        p.setPname("STUDENT");
        p=postDao.findOnePost(p);
        accountPostDao.add(student.getAid(),p.getId());

        if(studentDao.addStudent(student)==1)
            return true;
        return false;
    }

    @Override
    public Boolean updateStudent(Student student) {
        if (studentDao.updateStudent(student)==1)
            return true;
        return false;
    }

    @Override
    public Student findOneStudent(Student student) {
        Student s=studentDao.findOneStudent(student);
        return s;
    }


}
